package com.nnnu.wsnackshop.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.controller.base.BaseController;
import com.nnnu.wsnackshop.pojo.dto.ReviewCreateDTO;
import com.nnnu.wsnackshop.pojo.dto.ReviewUpdateDTO;
import com.nnnu.wsnackshop.pojo.vo.ReviewVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IReviewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户端-评价 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@RestController("userReviewsController")
@RequestMapping("/api/user/reviews")
@Tag(name = "用户端-评价接口")
@Slf4j
@RequiresRoles(value = {"user"}, logical = Logical.OR)
@RequiredArgsConstructor
public class ReviewsController extends BaseController {

    private final IReviewsService reviewsService;

    /**
     * 创建评价
     */
    @PostMapping
    @Operation(summary = "创建评价", description = "提交商品评价")
    public Result<ReviewVO> createReview(@RequestBody ReviewCreateDTO dto) {
        Integer userId = getUserId();
        log.info("创建评价: userId={}, productId={}, orderId={}", userId, dto.getProductId(), dto.getOrderId());

        ReviewVO review = reviewsService.createReview(
                userId,
                dto.getProductId(),
                dto.getOrderId(),
                dto.getSkuId(),
                dto.getRating(),
                dto.getComment(),
                dto.getImages());

        return Result.success(review);
    }

    /**
     * 更新评价
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新评价", description = "修改已有评价")
    public Result<ReviewVO> updateReview(@PathVariable Integer id, @RequestBody ReviewUpdateDTO dto) {
        Integer userId = getUserId();
        log.info("更新评价: userId={}, reviewId={}", userId, id);

        ReviewVO review = reviewsService.updateReview(
                id,
                userId,
                dto.getRating(),
                dto.getComment(),
                dto.getImages());

        return Result.success(review);
    }

    /**
     * 删除评价
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除评价", description = "删除已有评价")
    public Result<Boolean> deleteReview(@PathVariable Integer id) {
        Integer userId = getUserId();
        log.info("删除评价: userId={}, reviewId={}", userId, id);

        boolean result = reviewsService.deleteReview(id, userId);
        return Result.success(result);
    }

    /**
     * 获取用户的评价列表
     */
    @GetMapping("/my")
    @Operation(summary = "获取用户评价列表", description = "获取当前用户的所有评价")
    public Result<List<ReviewVO>> getUserReviews(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size) {

        Integer userId = getUserId();
        log.info("获取用户评价列表: userId={}, page={}, size={}", userId, page, size);

        List<ReviewVO> reviews = reviewsService.listUserReviews(userId, page, size);
        return Result.success(reviews);
    }

    /**
     * 获取订单的评价列表
     */
    @GetMapping("/order/{orderId}")
    @Operation(summary = "获取订单评价列表", description = "获取指定订单的所有评价")
    public Result<List<ReviewVO>> getOrderReviews(@PathVariable Long orderId) {
        Integer userId = getUserId();
        log.info("获取订单评价列表: userId={}, orderId={}", userId, orderId);

        List<ReviewVO> reviews = reviewsService.listReviewsByOrder(orderId);
        return Result.success(reviews);
    }

    /**
     * 获取订单商品的评价
     */
    @GetMapping("/order/{orderId}/product/{productId}")
    @Operation(summary = "获取订单商品评价", description = "获取指定订单的指定商品评价")
    public Result<ReviewVO> getOrderProductReview(@PathVariable Long orderId, @PathVariable Integer productId) {
        Integer userId = getUserId();
        log.info("获取订单商品评价: userId={}, orderId={}, productId={}", userId, orderId, productId);

        ReviewVO review = reviewsService.getReviewByOrderAndProduct(orderId, productId);
        return Result.success(review);
    }

    /**
     * 获取商品评价
     *
     * @param productId 商品ID
     * @param limit     获取数量
     * @return 商品评价
     */
    @GetMapping("/product/{productId}")
    public Result<?> getReviewsByProduct(@Parameter(description = "商品ID") @PathVariable Integer productId,
                                         Integer limit) {
        log.info("获取商品评价: productId={}", productId);
        List<ReviewVO> reviews = reviewsService.listReviewsByProduct(productId, limit);
        return Result.success(reviews);
    }

    /**
     * 获取商品分页评价
     *
     * @param productId 商品ID
     * @param page      页码
     * @param pageSize  每页大小
     * @param minRating 最小评分
     * @param maxRating 最大评分
     * @param hasImages 是否有图片
     * @return 评价分页结果
     */
    @GetMapping("/product/{productId}/page")
    @Operation(summary = "获取商品分页评价", description = "分页获取指定商品的评价")
    public Result<?> getProductReviewPage(
            @Parameter(description = "商品ID") @PathVariable Integer productId,
            @Parameter(description = "页码") @RequestParam(required = false) Integer page,
            @Parameter(description = "每页大小") @RequestParam(required = false) Integer pageSize,
            @Parameter(description = "最小评分") @RequestParam(required = false) Integer minRating,
            @Parameter(description = "最大评分") @RequestParam(required = false) Integer maxRating,
            @Parameter(description = "是否有图片") @RequestParam(required = false) Boolean hasImages) {

        log.info("获取商品分页评价: productId={}, page={}, pageSize={}", productId, page, pageSize);

        Page<ReviewVO> reviewPage = reviewsService.pageReviewsByProduct(
                productId, page, pageSize, minRating, maxRating, hasImages);

        return Result.success(reviewPage);
    }

    /**
     * 获取商品评价统计信息
     *
     * @param productId 商品ID
     * @return 评价统计信息
     */
    @GetMapping("/stats/{productId}")
    @Operation(summary = "获取商品评价统计", description = "获取指定商品的评价统计信息")
    public Result<?> getProductReviewStats(
            @Parameter(description = "商品ID") @PathVariable Integer productId) {

        log.info("获取商品评价统计: productId={}", productId);

        Map<String, Object> stats = reviewsService.getReviewStats(productId);

        return Result.success(stats);
    }

    /**
     * 获取评价详情
     *
     * @param id 评价ID
     * @return 评价详情
     */
    @Operation(summary = "获取评价详情", description = "获取指定评价的详细信息")
    @GetMapping("/{id}")
    public Result<?> getReviewDetail(@PathVariable Integer id) {
        log.info("获取评价详情: id={}", id);
        return Result.success(reviewsService.getReviewDetail(id), MessageConstants.SELECT_OK);
    }
}